package cn.xuan.zp.service.role.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xuan.zp.db.domain.Role;
import cn.xuan.zp.db.domain.RoleExample;
import cn.xuan.zp.db.domain.User;
import cn.xuan.zp.db.domain.UserRole;
import cn.xuan.zp.db.domain.UserRoleExample;
import cn.xuan.zp.db.domain.UserRoleExample.Criteria;
import cn.xuan.zp.db.mapper.RoleMapper;
import cn.xuan.zp.db.mapper.UserRoleMapper;
import cn.xuan.zp.service.role.IRoleService;

@Service
public class RoleServiceImpl implements IRoleService {
	
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	RoleMapper roleMapper;

	@Override
	public int addEnity(Role enity) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateEnity(Role enity) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Role getByPrimaryKey(long id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryEnity(Object... params) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Role> queryRoleByUser(User user) throws Exception {
		
		List<Role> llst = new ArrayList<>();
		
		UserRoleExample example = new UserRoleExample();
		
		Criteria criteria = example.createCriteria();
		
		criteria.andUserIdEqualTo(user.getId());
		
		List<UserRole> lst = userRoleMapper.selectByExample(example );
		
		if(lst != null && lst.size() > 0) {
			List<Long> roleIds = new ArrayList<>(); 
			for (UserRole userRole : lst) {
				long roleid = userRole.getRoleId();
				roleIds.add(roleid);
			}
			RoleExample exampleRole = new RoleExample();
			
			cn.xuan.zp.db.domain.RoleExample.Criteria criteriaRole = exampleRole.createCriteria();
			
			criteriaRole.andIdIn(roleIds);
			
			llst = roleMapper.selectByExample(exampleRole );
		}
		
		return llst;
	}

}
